package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.ivTest_chart_report.IvTestChartReportBean;
import com.navi.bean.out.ivTest_chart_report.IvTestChartReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IIvTestChartReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.maxBy;

@Slf4j
@Service
public class IvTestChartReportServiceImpl implements IIvTestChartReportService {


    public IvTestChartReportBean getGridData(ConditionBean conditions) {
        IvTestChartReportBean ivTestChartReportBean = new IvTestChartReportBean();

        //查询Chart图上的最大、最小、中心线
        final SqlVo sqlVo1 = new SqlVo();
        sqlVo1.appendSql("SELECT");
        sqlVo1.appendSql("A.*");
        sqlVo1.appendSql("FROM icim_jn.iv_data_set A");
        sqlVo1.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getLot_number())) sqlVo1.appendSql("AND A.lot_number in" + jointInSql(conditions.getLot_number()));
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo1.appendSql("AND A.tool_id_fk in" + jointInSql(conditions.getTool_id()));

        List<IvTestChartReportGridOut> resultDataList_1 = mySql_QueryRunner_QueryListBySql(sqlVo1, IvTestChartReportGridOut.class);

        //取时间跨度最大的一笔数据作为Chart上的最大、最小、中心线
        IvTestChartReportGridOut gridOut = resultDataList_1.stream().filter(data -> data.getGenerate_timestamp() != null)
                //.map(data -> {
                //    if (data.getUpdate_timestamp() == null) {
                //        data.setUpdate_timestamp(new Timestamp(System.currentTimeMillis()));
                //    }
                //    return data;
                //})
                //.collect(maxBy(comparing(data -> data.getUpdate_timestamp().getTime() - data.getGenerate_timestamp().getTime()))).orElseGet(IvTestChartReportGridOut::new);
                .collect(maxBy(comparing(data -> data.getGenerate_timestamp().getTime()))).orElseGet(IvTestChartReportGridOut::new);
        ivTestChartReportBean.setCenter_pm_value(gridOut.getCenter_pm_value());
        ivTestChartReportBean.setMax_pm_value(gridOut.getMax_pm_value());
        ivTestChartReportBean.setMin_pm_value(gridOut.getMin_pm_value());



        //查询Chart图的源数据
        final SqlVo sqlVo2 = new SqlVo();
        sqlVo2.appendSql("SELECT");
        sqlVo2.appendSql("b_1.tool_dsc as tool_id,");
        sqlVo2.appendSql("B.*");
        sqlVo2.appendSql("FROM icim_jn.iv_tester_data B");
        sqlVo2.appendSql("LEFT JOIN (select tool_id,tool_dsc from icim_jn.bis_tool ) b_1 ON B.tool_id_fk = b_1.tool_id");
        sqlVo2.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getLot_number())) sqlVo2.appendSql("AND B.lot_number in" + jointInSql(conditions.getLot_number()));
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo2.appendSql("AND B.tool_id_fk in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo2.appendSqlAndAddParam("AND B.TEST_TIME >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo2.appendSqlAndAddParam("AND B.TEST_TIME <= ?", conditions.getEnd_time());
        sqlVo2.appendSql("ORDER BY B.TEST_TIME");

        List<IvTestChartReportGridOut> resultDataList_2 = mySql_QueryRunner_QueryListBySql(sqlVo2, IvTestChartReportGridOut.class);
        ivTestChartReportBean.setGridData(resultDataList_2);
        return ivTestChartReportBean;
    }



}
